/**
 ***********************************************************************
 * Program name: StringValidator.java<br>
 * Program description: TODO<br>
 * Author: sugen<br>
 * Revision history:1.0<br>
 * <br>
 * Description of Change	Programmer		Date<br>
 * ---------------------    ----------      ----<br>
 * Initial creation			Su Gen		   2011-3-9 <br>
 * <br>
 * Copyright (c) 2011 by SUGEN ALL RIGHTS RESERVED<br>
 ************************************************************************
 */
package com.common;

import java.text.ParseException;
import java.text.SimpleDateFormat;

public final class StringValidator {

	public StringValidator() {
	}

	public static boolean isBlankOrNull(String s) {
		return s == null || s.trim().length() == 0;
	}

	public static boolean isAlphaNumeric(String s) {
		byte abyte0[] = s.getBytes();
		for (int i = 0; i < abyte0.length; i++)
			if ((abyte0[i] < 48 || abyte0[i] > 57)
					&& (abyte0[i] < 65 || abyte0[i] > 90)
					&& (abyte0[i] < 97 || abyte0[i] > 122))
				return false;

		return true;
	}

	public static boolean isNumeric(String numericStr) {
		byte abyte0[] = numericStr.getBytes();
		for (int i = 0; i < abyte0.length; i++)
			if (abyte0[i] < 48 || abyte0[i] > 57)
				return false;

		return true;
	}

	public static boolean isByte(String byteStr) {
		Byte byte1;
		try {
			byte1 = new Byte(byteStr);
		} catch (Exception exception) {
			return false;
		}
		return true;
	}

	public static boolean isShort(String shortStr) {
		Short short1;
		try {
			short1 = new Short(shortStr);
		} catch (Exception exception) {
			return false;
		}
		return true;
	}

	public static boolean isInt(String intStr) {
		Integer integer;
		try {
			integer = new Integer(intStr);
		} catch (Exception exception) {
			return false;
		}
		return true;
	}

	public static boolean isLong(String longStr) {
		Long long1;
		try {
			long1 = new Long(longStr);
		} catch (Exception exception) {
			return false;
		}
		return true;
	}

	public static boolean isFloat(String floatStr) {
		Float float1;
		try {
			float1 = new Float(floatStr);
		} catch (Exception exception) {
			return false;
		}
		return true;
	}

	public static boolean isDouble(String s) {
		Double double1;
		try {
			double1 = new Double(s);
		} catch (Exception exception) {
			return false;
		}
		return true;
	}

	public static boolean isDate(String s, String s1) {
		return isDate(s, s1, false);
	}

	public static boolean isDate(String dateStr, String dateFormat, boolean flag) {
		boolean flag1 = true;
		if (dateStr != null && dateFormat != null && dateFormat.length() > 0)
			try {
				SimpleDateFormat simpledateformat = new SimpleDateFormat(
						dateFormat);
				simpledateformat.setLenient(false);
				java.util.Date date = simpledateformat.parse(dateStr);
				if (flag && dateFormat.length() != dateStr.length())
					flag1 = false;
			} catch (ParseException parseexception) {
				flag1 = false;
			}
		else
			flag1 = false;
		return flag1;
	}

	public static boolean isInRange(String str, int low, int high) {
		long l = (new Long(str)).longValue();
		if (l >= (long) low && l <= (long) high)
			return true;
		return false;
	}

	public static boolean isInRange(int dest, int low, int high) {
		return dest >= low && dest <= high;
	}

	public static boolean isEmail(String email) {
		email = email.trim();
		int i = email.indexOf(64);
		int j = email.lastIndexOf(46);
		if (i < 1)
			return false;
		if (j < i)
			return false;
		return email.length() - j > 2;
	}

	public static boolean isPhone(String telephoneStr) {
		if (telephoneStr != null) {
			telephoneStr = telephoneStr.trim();
			if (isNumeric(telephoneStr) && telephoneStr.length() > 5
					&& telephoneStr.length() < 20)
				return true;
		}
		return false;
	}

	public static boolean isMatch(String s, String s1) {
		if (s != null || s1 != null)
			return s.equals(s1);
		else
			return false;
	}

	public static boolean isValidIDate(String s) {
		if (s == null || !isNumeric(s))
			return false;
		if (s.length() != 8 && s.length() != 14)
			return false;
		if (s.length() == 14) {
			if (Integer.parseInt(s.substring(8, 10)) > 24)
				return false;
			if (Integer.parseInt(s.substring(10, 12)) > 60)
				return false;
			if (Integer.parseInt(s.substring(12, 14)) > 60)
				return false;
		}
		int i = Integer.parseInt(s.substring(0, 4));
		int j = Integer.parseInt(s.substring(4, 6));
		int k = Integer.parseInt(s.substring(6, 8));
		byte byte0 = 31;
		if (j < 1 || j > 12)
			return false;
		if (j == 4 || j == 6 || j == 9 || j == 11)
			byte0 = 30;
		else if (j == 2)
			if (i % 400 == 0)
				byte0 = 29;
			else if (i % 100 == 0)
				byte0 = 28;
			else if (i % 4 == 0)
				byte0 = 29;
			else
				byte0 = 28;
		return k >= 1 && k <= byte0;
	}

	public static boolean isShorterThan(String str, int len) {
		return str.length() < len;
	}
}